home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / string / strxfrm.c < prev    next >
C/C++ Source or Header  |  1991-06-12  |  2KB  |  77 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <localeinfo.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25.  
  26. /* Transform SRC into a form such that the result of strcmp
  27.    on two strings that have been transformed by strxfrm is
  28.    the same as the result of strcoll on the two strings before
  29.    their transformation.  The transformed string is put in at
  30.    most N characters of DEST and its length is returned.  */
  31. size_t
  32. DEFUN(strxfrm, (dest, src, n), char *dest AND CONST char *src AND size_t n)
  33. {
  34.   CONST unsigned char *CONST values
  35.     = _collate_info != NULL ? _collate_info->values : NULL;
  36.   CONST unsigned char *CONST offsets
  37.     = _collate_info != NULL ? _collate_info->offsets : NULL;
  38.   register size_t done = 0;
  39.  
  40.   while (*src != '\0')
  41.     {
  42.       CONST unsigned char c = *src++;
  43.  
  44.       ++done;
  45.       if (offsets != NULL && offsets[c] != 0)
  46.     {
  47.       ++done;
  48.       if (offsets[c] == CHAR_MAX)
  49.         ++done;
  50.     }
  51.       if (done < n && dest != NULL)
  52.     {
  53.       if (values == NULL)
  54.         *dest++ = c;
  55.       else if (values[c] == UCHAR_MAX && offsets[c] == 0)
  56.         /* This is a non-collating element.  Skip it.  */
  57.         ;
  58.       else if (values[c] == UCHAR_MAX && offsets[c] == CHAR_MAX)
  59.         {
  60.           /* This element collates lower than anything else.  */
  61.           *dest++ = '\001';
  62.           *dest++ = '\001';
  63.           *dest++ = '\001';
  64.         }
  65.       else
  66.         {
  67.           *dest++ = values[c];
  68.           *dest++ = offsets[c];
  69.         }
  70.     }
  71.     }
  72.  
  73.   if (dest != NULL)
  74.     *dest = '\0';
  75.   return done;
  76. }
  77.